package com.sansui.blog.service.impl;

import com.sansui.blog.dao.TypeDao;
import com.sansui.blog.pojo.Type;
import com.sansui.blog.service.TypeService;
import com.sansui.blog.utils.TreeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @name: TypeServiceImpl
 * @author: Sansui
 * @Software: IDEA
 * @Blog: https://www.cnblogs.com/sansui6/
 * @date: 2022-03-21 21:18
 **/
@Service
public class TypeServiceImpl implements TypeService {


    @Autowired
    private TypeDao typeDao;


    /**
     * 获取后台 所有分类信息
     * @return 所有分类信息
     * Integer page,Integer limit
     */

    @Override
    public Map<String, Object> getAllTypeByPage(Integer page,Integer limit) {
        Map<String,Object> data = new HashMap<>(16);
        Integer pages = (page-1)*limit;
        List<Type> types = typeDao.findAllType(pages,limit);
        Integer typeCount = typeDao.CountType();
        data.put("code",0);
        data.put("msg","查询成功！");
        data.put("count",typeCount);
        data.put("data",types);
        return data;
    }


    /**
     * 根据typeid获取分类信息
     * @return 分类信息
     */
    @Override
    public Type getTypeById(Long typeId) {
//        Type type = typeDao.getTypeById(typeId);
        return typeDao.getTypeById(typeId);
    }


    /**
     * 根据name获取分类信息
     * @return 分类信息
     */
    @Override
    public Map<String, Object> TypeSearchByName(String typeName) {
        Map<String,Object> data = new HashMap<>(16);
        List<Type> types = typeDao.TypeSearchByName(typeName);
        Integer typeCount = typeDao.CountTypeByLike(typeName);
        data.put("code",0);
        data.put("msg","查询成功！");
        data.put("count",typeCount);
        data.put("data",types);
        return data;
    }

    /**
     * 删除分类信息
     * @return 是否删除成功
     */
    @Transactional
    @Override
    public Map<String, Object> deleteType(Long typeId) {
        Integer deleteTypeNum = typeDao.deleteType(typeId);
        Map<String,Object> data = new HashMap<>(16);
        if(deleteTypeNum != 0){
            data.put("code",200);
            data.put("msg","删除成功！");
        }else{
            data.put("code",400);
            data.put("msg","删除失败！");
        }
        return data;
    }

    /**
     * 批量删除分类信息
     * @return 批量删除分类信息
     */
    @Transactional
    @Override
    public Map<String, Object> deleteTypeByIds(List<String> delList) {
        Map<String,Object> data = new HashMap<>(16);
        try{
            typeDao.deleteTypeByIds(delList);
            data.put("code",200);
            data.put("msg","批量删除成功！");
        }catch (Exception e){
            data.put("code",400);
            data.put("msg","批量删除失败！");
//            System.out.println(delList);
        }
        return data;
    }

    /**
     * 修改分类信息
     * @return 修改分类信息
     */
    @Transactional
    @Override
    public  Map<String, Object> updateType(String typeName,Long typeId,String username){
        Map<String,Object> data = new HashMap<>(16);
        if (typeName == null) {
            data.put("code",500);
            data.put("msg","修改失败，分类数据不可为空！");
            return data;
        }

        Type type = getTypeById(typeId);
        if(type == null){
            data.put("code",500);
            data.put("msg","修改失败，分类不存在！");
            return data;
        }

        Integer typeNum = typeDao.findTypeByTypeName(typeName);
        if(typeNum > 0){
            data.put("code",400);
            data.put("msg","修改失败，该分类已存在！");
            return data;
        }

        type.setTypeName(typeName);
        type.setUpdateTime(new Date());
        type.setUpdateUser(username);

        Integer updateNum = typeDao.updateType(type);
        if(updateNum != 1){
            data.put("code",400);
            data.put("msg","修改失败！");
            return data;
        }

        data.put("code",200);
        data.put("msg","修改成功！");
        return data;

    }

    /**
     * 保存分类信息
     * @return 保存分类信息
     */
    @Transactional
    @Override
    public Map<String, Object> addType(String typeName,String username) {
        Map<String,Object> data = new HashMap<>(16);

        if (typeName == null) {
            data.put("code",500);
            data.put("msg","添加失败，分类数据不可为空！");
            return data;
        }

        Integer typeNum = typeDao.findTypeByTypeName(typeName);
        if(typeNum > 0){
            data.put("code",400);
            data.put("msg","添加失败，该分类已存在！");
            return data;
        }
//        new SimpleDateFormat ("yyyy-MM-dd hh:mm:ss").format(new Date())
        Type type = new Type();
        type.setTypeName(typeName);
        type.setCreateTime(new Date());
        type.setUpdateTime(new Date());
        type.setCreateUser(username);
        type.setUpdateUser(username);

        Integer addNum = typeDao.addtype(type);
        if(addNum != 1){
            data.put("code",400);
            data.put("msg","添加失败！");
            return data;
        }

        data.put("code",200);
        data.put("msg","添加成功！");
        return data;
    }


    /**
     * 博客首页，右侧加载分类名称及博客数量
     * 博客数量 -- 分类名称
     * @return map集合
     */
    @Override
    public List<Map<String, Object>> countTypeInfo(Integer isPage) {
        return typeDao.countTypeInfo(isPage);
    }

}
